#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include "faceprocessor.h"
#include "audioprocessor.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    imageCapture = new ImageCapture(this);
    imageProcess =new ImageProcess(this);
    countFrame=0;


    //QObject::connect(workThread,SIGNAL(display(QPixmap)),this,SLOT(showFrame(QPixmap)));
    QObject::connect(imageCapture,SIGNAL(display(cv::Mat)),this,SLOT(showFrame(cv::Mat)));
    QObject::connect(imageProcess,SIGNAL(display(cv::Mat)),this,SLOT(showFace(cv::Mat)));
    QObject::connect(imageProcess,SIGNAL(display(QString,quint8)),this,SLOT(showText(QString,quint8)));

    filterImage = Mat(480,640,CV_8UC3,Scalar(0,0,0));
    cv::Mat filterImageCenter = filterImage(Rect(filterImage.cols/2 -150,filterImage.rows/2 -150,300,300));
    filterImageCenter.setTo(100);
}

MainWindow::~MainWindow()
{
    delete ui;
    if(imageCapture!=NULL)
        delete imageCapture;
    if(imageProcess!=NULL)
        delete imageProcess;
}

void MainWindow::on_actionOpen_triggered()
{
    imageCapture->start();
    qDebug()<< "摄像线程启动完毕";
}

void MainWindow::showFrame(QPixmap pixmap){
    ui->pictureBox->setPixmap(pixmap);
    ui->pictureBox->repaint();

}



void MainWindow::roiFrame(cv::Mat &frame, quint8 width, quint8 height){

    cv::Rect roiRect=  Rect(frame.cols/2 -150,frame.rows/2 -150,width,height);
    frame = frame(roiRect);

}

void MainWindow::showText(QString txt,quint8 type){


    if(type ==1 ){
        ui->labelRes1->setText(txt);
    }else if(type == 2){
       ui->labelRes2->setText(txt);
     }



}

void MainWindow::showFace(cv::Mat faceFrame){
    ui->pictureBoxLittle->setPixmap(QPixmap::fromImage(
                               QImage(
                                   faceFrame.data,
                                   faceFrame.cols,
                                   faceFrame.rows,
                                   faceFrame.step,
                                   QImage::Format_RGB888
                                   ).scaled(ui->pictureBoxLittle->size()).rgbSwapped()));
    ui->pictureBoxLittle->repaint();



}

void MainWindow::showFrame(cv::Mat frame){



    cv::Mat littleFrame = frame(Rect(frame.cols/2 -150,frame.rows/2 -150,300,300)).clone();

    // 启动处理线程
    if(countFrame < 5)
        countFrame ++;
    else {
        countFrame = 0;
        imageProcess->setLittlePicture(littleFrame);
        imageProcess->start();
        qDebug()<< "处理线程启动完毕";
    }
    // 显示摄像头图像

    addWeighted(frame,0.5, filterImage, 0.8,0.0,frame);

    ui->pictureBox->setPixmap(QPixmap::fromImage(
                               QImage(
                                   frame.data,
                                   frame.cols,
                                   frame.rows,
                                   frame.step,
                                   QImage::Format_RGB888
                                   ).rgbSwapped()));
    ui->pictureBox->repaint();




}


void MainWindow::on_actionRegistryFromDir_triggered()
{
    qDebug()<< "开始读入黑名单图片";
    QDir dir;
    QString pathName= QFileDialog::getExistingDirectory(this, tr("Open Directory") ,
                                      "/home/fz/wj-work/face/EasyFace-main/images/register", QFileDialog::ShowDirsOnly
                                      |QFileDialog::DontResolveSymlinks);
    qDebug()<<pathName;

    imageProcess->processor.setRegisterImagepath(pathName);
    imageProcess->processor.regis();
    QMessageBox::information(this, tr("info"),tr("注册完毕"));
}

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::on_actionClose_triggered()
{
    imageCapture->terminate();
    imageCapture->wait();
    qDebug()<< "摄像线程中止完毕";
}

void MainWindow::on_actionOpenMic_triggered()
{
    //AudioProcessor.main();

}
